﻿using DH.MQ;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using DH.Common;

namespace TestMQ
{
    class Program
    {
        public class Message : BaseMessage
        {
            public string MessageValue { get; set; }
            public override BaseMessage GetMessageResult(string bm)
            {
                BaseMessage result = Newtonsoft.Json.JsonConvert.DeserializeObject<Message>(bm);
                return result;
            }
        }
        private static void ConsoleResult(BaseMessage ms)
        {

            Message mst = (Message)ms;
            //获取订单
            Order order = Newtonsoft.Json.JsonConvert.DeserializeObject<Order>(mst.MessageValue);

            //List<Product> list = order.PorductList;
            //Console.WriteLine("开始发送锁定库存的消息请求");
            //RMQProvider provider = new RMQProvider();
            //var message = Newtonsoft.Json.JsonConvert.SerializeObject(list);
            //Message bm = new Message();
            //bm.MessageValue = message;
            //string messageID = "";
            //provider.SendMessage(bm.BuildMessage(bm, out messageID), "lu9.product.1234", false);

            Console.WriteLine("订单编号为："+order.osn);
            Console.WriteLine("模拟保存订单");
            Console.WriteLine("开始发送减库存的消息请求");
            //Console.WriteLine(" [x] Received :'{0}'", mst.MessageValue);
        }
        static void Main(string[] args)
        {
            Console.WriteLine(System.Configuration.ConfigurationManager.AppSettings["ListenName"].ToString());
            Console.WriteLine("开始监听处理订单……");
            Action<BaseMessage> action = ConsoleResult;
            RMQProvider rp = new RMQProvider();
            rp.act = action;
            rp.basemessage = new Message();
            rp.act = ConsoleResult;
            rp.ListenQueueName = System.Configuration.ConfigurationManager.AppSettings["ListenName"].ToString();
            rp.OnListening();
            Console.Read();

        }
    }
    class Order
    {
        /// <summary>
        /// 订单编号
        /// </summary>
        public string osn { get; set; }
        /// <summary>
        /// 用户ID
        /// </summary>
        public int uid { get; set; }
        /// <summary>
        /// 订单金额
        /// </summary>
        public decimal Money { get; set; }
        public List<Product> PorductList = new List<Product>();
    }

    class Product
    {
        /// <summary>
        /// 商品编号
        /// </summary>
        public int ProductID { get; set; }
    }

    class Confirm
    {

    }
}
